route.test.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* @vitest-environment node */
  2. import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
  3. import fs from "node:fs/promises";
  4. import os from "node:os";
  5. import path from "node:path";
  6. vi.mock("@/lib/auth/session", () => ({
  7. getSession: vi.fn(),
  8. }));
  9. import { getSession } from "@/lib/auth/session";
  10. import { GET } from "./route.js";
  11. describe("GET /api/branches/[branch]/[year]/[month]/days", () => {
  12. let tmpRoot;
  13. const originalNasRoot = process.env.NAS_ROOT_PATH;
  14. beforeEach(async () => {
  15. vi.clearAllMocks();
  16. tmpRoot = await fs.mkdtemp(path.join(os.tmpdir(), "api-days-"));
  17. process.env.NAS_ROOT_PATH = tmpRoot;
  18. await fs.mkdir(path.join(tmpRoot, "NL01", "2024", "10", "23"), {
  19. recursive: true,
  20. });
  21. });
  22. afterEach(async () => {
  23. process.env.NAS_ROOT_PATH = originalNasRoot;
  24. if (tmpRoot) await fs.rm(tmpRoot, { recursive: true, force: true });
  25. });
  26. it("returns 401 when unauthenticated", async () => {
  27. getSession.mockResolvedValue(null);
  28. const res = await GET(
  29. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  30. {
  31. params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }),
  32. }
  33. );
  34. expect(res.status).toBe(401);
  35. expect(await res.json()).toEqual({ error: "Unauthorized" });
  36. });
  37. it("returns 403 when branch user accesses a different branch", async () => {
  38. getSession.mockResolvedValue({
  39. role: "branch",
  40. branchId: "NL01",
  41. userId: "u1",
  42. });
  43. const res = await GET(
  44. new Request("http://localhost/api/branches/NL02/2024/10/days"),
  45. {
  46. params: Promise.resolve({ branch: "NL02", year: "2024", month: "10" }),
  47. }
  48. );
  49. expect(res.status).toBe(403);
  50. expect(await res.json()).toEqual({ error: "Forbidden" });
  51. });
  52. it("returns days for a valid branch/year/month when allowed", async () => {
  53. getSession.mockResolvedValue({
  54. role: "admin",
  55. branchId: null,
  56. userId: "u2",
  57. });
  58. const res = await GET(
  59. new Request("http://localhost/api/branches/NL01/2024/10/days"),
  60. {
  61. params: Promise.resolve({ branch: "NL01", year: "2024", month: "10" }),
  62. }
  63. );
  64. expect(res.status).toBe(200);
  65. const body = await res.json();
  66. expect(body).toEqual({
  67. branch: "NL01",
  68. year: "2024",
  69. month: "10",
  70. days: ["23"],
  71. });
  72. });
  73. it("returns 400 when any param is missing (authenticated)", async () => {
  74. getSession.mockResolvedValue({
  75. role: "admin",
  76. branchId: null,
  77. userId: "u2",
  78. });
  79. const res = await GET(
  80. new Request("http://localhost/api/branches/NL01/2024//days"),
  81. {
  82. params: Promise.resolve({
  83. branch: "NL01",
  84. year: "2024",
  85. month: undefined,
  86. }),
  87. }
  88. );
  89. expect(res.status).toBe(400);
  90. expect(await res.json()).toEqual({
  91. error: "branch, year oder month fehlt",
  92. });
  93. });
  94. });